import { Component, OnInit } from '@angular/core'
import { JsonApiService } from 'app/core/api/json-api.service' //http请求配置引入
import { Router, ActivatedRoute } from '@angular/router' //路由引入
import { config } from '../../shared/smartadmin.config'

declare var $: any
@Component({
  selector: 'sa-installer-statistics',
  templateUrl: './installer-statistics.component.html',
  styleUrls: ['./installer-statistics.css']
})
export class InstallerStatisticsComponent implements OnInit {
  tableData: any[] = [] //table数据
  tableParam: any = {
    searchKey: '',
    startTime: '',
    endTime: '',
    sortBy: '',
    sortType: ''
  }

  // 添加公司的数据
  selectedCompanyName: any = ''
  selectedCompanyId: any = ''

  companyArrays: any = []

  isRfid: any = false //  是否是带RFID的公司（0不是 1是）

  inputApi: string = 'company/getParentCompany.html'

  addRecordCompanySelected(data) {
    if(data.companyIdSelectedList.length > 0){
      if(data.companyIdSelectedList[0] != undefined){
        this.selectedCompanyId = data.companyIdSelectedList.join(',')
      }else{
        this.selectedCompanyId = ''
      }
      
    }else{
      this.selectedCompanyId = ''
    }
    
  }

  constructor(private jsonApiService: JsonApiService, private router: Router, private activatedRoute: ActivatedRoute) {
    activatedRoute.queryParams.subscribe(queryParams => {
      // this.curPage = queryParams.nowPage || 1;
    })
  }
  ngOnInit() {
    // this.load()
  }
  getSelectTableTime(time) {
    this.tableParam.startTime = time.startTime
    this.tableParam.endTime = time.endTime
    this.load()
  }

  //点击对接厂区
	companySelectedClick() {
		$('#companyName .s_tree_box.s_tree_box_width').addClass('tree_open')
	}

  // 快捷排序
  columnSortFlag: string = ''
  columnSort(val) {
    this.columnSortFlag = val
    this.tableParam.sortBy = val.split('-')[0]
    this.tableParam.sortType = val.split('-')[1]
    if (this.selectedCompanyId || (this.tableParam.startTime && this.tableParam.endTime)) {
      this.statistics()
    } else {
      this.load()
    }
  }
  //初始化加载list数据
  load() {
    let data = {
      parameters: {
        // searchKey: common.stringTrim(this.tableParam.searchKey),
        startTime: this.tableParam.startTime,
        endTime: this.tableParam.endTime,
        allCompanyId: this.selectedCompanyId,
        sortBy: this.tableParam.sortBy,
        sortType: this.tableParam.sortType,
        type: '1'
      }
    }
    let reqdata = 'gridPager=' + JSON.stringify(data)
    this.jsonApiService.http('installerStatistics/installerCompanyStatistics.html', reqdata).subscribe(result => {
      if (result.success && result.data) {
        for (var i = 0; i < result.data.length; i++) {
          result.data[i].sensorRepairedPct =
            result.data[i].sensorRepairedPct != 0 ? (result.data[i].sensorRepairedPct * 100).toFixed(1) + '%' : 0
          result.data[i].sensorRepairedCountPct =
            result.data[i].sensorRepairedCountPct != 0
              ? (result.data[i].sensorRepairedCountPct * 100).toFixed(1) + '%'
              : 0
          result.data[i].sensorRepairPct =
            result.data[i].sensorRepairPct != 0 ? (result.data[i].sensorRepairPct * 100).toFixed(1) + '%' : 0
          result.data[i].sensorRepairCountPct =
            result.data[i].sensorRepairCountPct != 0 ? (result.data[i].sensorRepairCountPct * 100).toFixed(1) + '%' : 0
          result.data[i].gpsRepairCountPct =
            result.data[i].gpsRepairCountPct != 0 ? (result.data[i].gpsRepairCountPct * 100).toFixed(1) + '%' : 0
          result.data[i].sensorFlagPct =
            result.data[i].sensorFlagPct != 0 ? (result.data[i].sensorFlagPct * 100).toFixed(1) + '%' : 0
          result.data[i].sensorFlagCountPct =
            result.data[i].sensorFlagCountPct != 0 ? (result.data[i].sensorFlagCountPct * 100).toFixed(1) + '%' : 0
          result.data[i].gpsFlagPct =
            result.data[i].gpsFlagPct != 0 ? (result.data[i].gpsFlagPct * 100).toFixed(1) + '%' : 0
        }
      }
      this.tableData = result.data
    })
  }
  //table刷新
  refresh() {
    this.load()
  }
  //表格搜索
  tableSearch() {
    this.load()
  }
  // 数据导出
  exportData() {
    if (this.tableParam.startTime && this.tableParam.endTime) {
      if (!this.tableParam.sortBy) {
        this.tableParam.sortBy = '1'
        this.tableParam.sortType = '2'
        this.columnSortFlag = '1-2'
      }
    }
    var data: any
    data = {
      parameters: {
        allCompanyId: this.selectedCompanyId,
        startTime: this.tableParam.startTime,
        endTime: this.tableParam.endTime,
        sortBy: this.tableParam.sortBy,
        sortType: this.tableParam.sortType,
        type: '2'
      }
    }
    let timeCheck = common.correctTime(data.parameters, ['type'])
    if (!timeCheck.status) {
      this.jsonApiService.message(timeCheck.message)
      return
    }
    common.exprotDataFile(data, 'gridPager', config.BaseURL + 'installerStatistics/installerCompanyStatistics.html')
  }
  //统计
  statistics() {
    if (this.tableParam.startTime && this.tableParam.endTime) {
      if (!this.tableParam.sortBy) {
        this.tableParam.sortBy = '1'
        this.tableParam.sortType = '2'
        this.columnSortFlag = '1-2'
      }
    }
    let data = {
      parameters: {
        // searchKey: common.stringTrim(this.tableParam.searchKey),
        startTime: this.tableParam.startTime,
        endTime: this.tableParam.endTime,
        allCompanyId: this.selectedCompanyId,
        sortBy: this.tableParam.sortBy,
        sortType: this.tableParam.sortType,
        type: '1'
      }
    }
    let reqdata = 'gridPager=' + JSON.stringify(data)
    this.jsonApiService.http('installerStatistics/installerCompanyStatistics.html', reqdata).subscribe(result => {
      if (result.success && result.data) {
        for (var i = 0; i < result.data.length; i++) {
          result.data[i].sensorRepairedPct =
            result.data[i].sensorRepairedPct != 0 ? (result.data[i].sensorRepairedPct * 100).toFixed(1) + '%' : 0
          result.data[i].sensorRepairedCountPct =
            result.data[i].sensorRepairedCountPct != 0
              ? (result.data[i].sensorRepairedCountPct * 100).toFixed(1) + '%'
              : 0
          result.data[i].sensorRepairPct =
            result.data[i].sensorRepairPct != 0 ? (result.data[i].sensorRepairPct * 100).toFixed(1) + '%' : 0
          result.data[i].sensorRepairCountPct =
            result.data[i].sensorRepairCountPct != 0 ? (result.data[i].sensorRepairCountPct * 100).toFixed(1) + '%' : 0
          result.data[i].gpsRepairCountPct =
            result.data[i].gpsRepairCountPct != 0 ? (result.data[i].gpsRepairCountPct * 100).toFixed(1) + '%' : 0
          result.data[i].sensorFlagPct =
            result.data[i].sensorFlagPct != 0 ? (result.data[i].sensorFlagPct * 100).toFixed(1) + '%' : 0
          result.data[i].sensorFlagCountPct =
            result.data[i].sensorFlagCountPct != 0 ? (result.data[i].sensorFlagCountPct * 100).toFixed(1) + '%' : 0
          result.data[i].gpsFlagPct =
            result.data[i].gpsFlagPct != 0 ? (result.data[i].gpsFlagPct * 100).toFixed(1) + '%' : 0
        }
      }
      this.tableData = result.data
    })
  }
}
